package org.example.Servlet;

import org.example.Entity.User;
import org.example.Utils.DBUtil;
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.json.JSONObject;

@WebServlet("/GetUserServlet")
public class GetUserServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        JSONObject jsonObject = new JSONObject();
        if (user != null) {
            jsonObject.put("username", user.getUsername());
        } else {
            jsonObject.put("error", "用户未登录");
        }

        response.setContentType("application/json");
        response.getWriter().write(jsonObject.toString());
    }
}